var city = require('/assets/js/city.js');
Page({
  data: {
    id:null,
    focus: "",
    step:1,
    cityShow: false,
    cityData: [],
    range: [],
    citys: [],
    sexradio: ["男","女"],
    info: {
      "CustomerDemandId": 0,
      "Source": null,
      "IntentionName": "",
      "SourceName": "",
      "RenovationTypeName": "",
      "PromotionName": "",
      "BudgetName": "",
      "MeasureDateName": "",
      "PlanInTimeName": "",
      "ApartmentName": "",
      "ProvinceId": null,
      "ProvinceName": "",
      "CityId": null,
      "CityName": "",
      "CountyId": null,
      "CountyName": "",
      "CreateId": null,
      "HouseName": "",
      "HouseAddress": "",
      "IsBrand": false,
      "RenovationType": null,
      "Budget": null,
      "Promotion": null,
      "DemandProducts": "",
      "Concerns": "",
      "MeasureDate": null,
      "RecommendNo": null, 
      "IsSend": false,
      "IsAppoint": false,
      "Remark": "",	
      "LevelId": 0,
      "SoftDesignId": 0,
      "Customer": {
        "CustomerName": "",
        "Mb": null,
        "EmployeeId": null, 		
        "CustomerTypeId": null,
        "Sex": null,
        "Intention": null,
        "IsFinalCustomer": false,
        "Employee": {
          "EmployeeName": ""
        }
      },
      "RecommendEmployee": null,
      "RecommendCustomer": null,
      "DesignerId": null,
      "SoftDesignId": null,
      "LeaderId": null,
      "Designer": {},
      "SoftDesigner": {},
      "Leader": {},
      "IntentionName": "",
      "SourceName": "",
      "RenovationTypeName": "",
      "BudgetName": "",
      "MeasureDateName": "",
      "PlanInTimeName": "",
      "ApartmentName": "",
      "ConcernNames": "",
      "DemandProductNames": "",
    }
  },
  onLoad(e) {
    console.log(e);
    if(e.id){
      this.setData({
        id: e.id
      })
      this.getInfo()
    }else{
      var newlist=this.data.info
      newlist.Customer.EmployeeId = getApp().globalData.user.EmployeeId
      newlist.CreateId = getApp().globalData.user.EmployeeId
      newlist.Customer.Employee.EmployeeName = getApp().globalData.user.EmployeeName
    }
    this.setData({
      cityData: city.cityData,
      // City: city.cityData[this.data.range[0]].children,
      // County: city.cityData[this.data.range[0]].children[this.data.range[1]].children
    })
    
  },
  onReady() {
    dd.enableLeaveConfirm({
      effect: ['back', 'close'],
      info: {
        title: '离开确认：',
        content: '离开将导致内容丢失，确认离开吗？'
      },
      success: function(){
      },
      fail: function(){
      },
      complete: function(){}
    })
  },
  changeValue(e){
    var aftdata = this.data.info
    if(e.target.dataset.parent){
      aftdata[e.target.dataset.parent][e.target.dataset.key] = e.detail.value
      if(e.target.dataset.key == "CustomerName"){
        aftdata[e.target.dataset.key] = e.detail.value
      }
    }else{
      aftdata[e.target.dataset.key] = e.detail.value
    }
    this.setData({info:aftdata})
  },
  closeboard() {
    dd.hideKeyboard();
  },
  openboard(e) {
    var _this = this
    setTimeout(() => {
      _this.setData({focus:e.target.dataset.key})
    }, 100);
  },
  onGetid(){
    // this.setData({id:this.data.id+1})
    // console.log(this.data.id);
    console.log(1010);
    
  },
  toNd() {
    if (this.data.info.Source) {
      if((this.data.info.Source == 3 || this.data.info.Source == 4 || this.data.info.Source == 5) && !this.data.info.RecommendNo){
				dd.showToast({
          type: 'fail',
          content: '推荐人不能为空'
        });
        return;
			}
    }else{
      dd.showToast({
        type: 'fail',
        content: '客户来源不能为空'
      });
      return;
    }


    if(!this.data.info.Customer.CustomerName || !this.data.info.Customer.Mb){
      dd.showToast({
        type: 'fail',
        content: '客户名称、联系方式不能为空'
      });
      return;
    }

    this.setData({step:2})
  },
  getInfo() {
    var _this = this
    getApp().req({
      url: "/customer/customerdetail/"+this.data.id,
      cb(res){
        console.log(res);
        _this.setData({
          info: res.data
        })
      }
    })
    
  },
  toSlt(e) {
    console.log(e);
    var str = "?"
    for (const i in e.target.dataset) {
      str+= i+"="+e.target.dataset[i]+"&"
    }
    str = str.substr(0,str.length-1)
    if(str.length <= 1){
      return;
    }
    dd.navigateTo({
      url: "/pages/marks/sltradio/sltradio" + str
    })
  },
  toChk(e) {
    console.log(e);
    var str = "?"
    for (const i in e.target.dataset) {
      str+= i+"="+e.target.dataset[i]+"&"
    }
    if(str.length <= 2){
      return;
    }
    str += "chked=" + this.data[e.target.dataset.name]
    dd.navigateTo({
      url: "/pages/marks/sltchk/sltchk" + str
    })
  },
  sltRecommend() {
    if(this.data.info.Source == 3){
      dd.navigateTo({
        url: "/pages/marks/empslt/empslt?role=Source"
      })
    }else if(this.data.info.Source == 4 || this.data.info.Source == 5){
      dd.navigateTo({
        url: "/pages/marks/sltrecomm/sltrecomm"
      })
    }
  },
  toemp(e) {
    dd.navigateTo({
      url: "/pages/marks/empslt/empslt?role=" + e.target.dataset.name
    })
  },
  sexChange(e) {
    console.log(e);
    var newinfo = this.data.info
    newinfo.Customer.Sex = e.detail.value
    this.setData({info: newinfo})
  },
  citySlt() {
    this.setData({
      cityShow: true
    })
    console.log(this.data.cityShow);
  },
  toMark(e) {
    dd.navigateTo({
      url:"/pages/marks/empslt/empslt"
    })
  },
  onGetCity(e){
    console.log(e);
    var cityarr = [e[0][0],e[1][0],e[2][0]]
    var newinfo = this.data.info
    newinfo.ProvinceId = e[0][0]
    newinfo.ProvinceName = e[0][1]
    newinfo.CityId = e[1][0]
    newinfo.CityName = e[1][1]
    newinfo.CountyId = e[2][0]
    newinfo.CountyName = e[2][1]
    this.setData({
      cityShow: false,
      citys: cityarr,
      info: newinfo
    })
    console.log(this.data.info);
    
  },
  backOne() {
    this.setData({step:1})
  },
  chkChange(e) {
    console.log(e);
    var keyName = e.target.dataset.name
    var newlist = this.data.info
    if(e.detail.value.length){
      newlist[keyName] = true
    }else{
      newlist[keyName] = false
    }
    this.setData({info:newlist})
  },
  customerSave() {
    console.log(this.data.info);
    var _this = this
    // getApp().req({
    //   url: "/customer/customeredit",
    //   type: "POST",
    //   data: this.data.info,
    //   cb(res) {
    //     console.log(res);
        
    //     if(_this.data.info.CustomerDemandId == 0){
    //       dd.redirectTo({
    //         url: '../crmcustomer/crmcustomer'
    //       })
    //     }else{
    //       dd.navigateBack()
    //     }
    //   }
    // })
    
  }
});
